
import pymysql
class mysqlUtil:

    def __init__(self,user,passwd,host,port,db):
        self.user = user
        self.passwd = passwd
        self.host = host
        self.port = port
        self.db = db
        self.conn = self.get_connection()
        self.cursor =self.conn.cursor(pymysql.cursors.DictCursor)
    def get_connection(self):
        return pymysql.connect(
            host=self.host,
            user=self.user,
            passwd=self.passwd,
            port=self.port,
            db=self.db
        )

    def query(self,sql):
        print(self.cursor.execute(sql))

    def result(self):
        print(self.cursor.fetchall())

    def __del__(self):
        self.conn.close()
        self.cursor.close()

if __name__ == '__main__':

    mysql = mysqlUtil('jxz', '123456', '118.24.3.40', 3306, 'jxz')
    sql = "select * from students limit 3;"
    mysql.query(sql)
    mysql.result()
